package org.carabiner.infopanel;

import java.awt.Color;
import java.awt.Component;

import javax.swing.Icon;
import javax.swing.tree.DefaultMutableTreeNode;

import org.carabiner.util.ComponentUtils;

/**
 * A TreeNode representing a Component that has been inspected by Carabiner.
 * 
 * 
 * <p>
 * Carabiner Testing Framework
 * </p>
 * <p>
 * Copyright: <a href="http://www.gnu.org/licenses/gpl.html">GNU Public License</a>
 * </p>
 * 
 * @author Ben Rady (benrady@gmail.com)
 */
class CarabinerComponentNode extends DefaultMutableTreeNode
{
  private Icon m_Icon;

  private Component m_Component;

  private Color m_HighlightColor;

  /**
   * Initializes a new instance of the <code>IconComponentNode</code> class.
   * 
   * @param userObject
   *          Component
   */
  public CarabinerComponentNode(Component userObject)
  {
    this(userObject, true, null, null);
    m_Component = userObject;
  }

  /**
   * Initializes a new instance of the <code>CarabinerMessageNode</code>
   * class.
   * 
   * @param userObject
   *          Component
   * @param icon
   *          Icon
   */
  public CarabinerComponentNode(Component userObject, Icon icon)
  {
    this(userObject, true, icon, null);
  }

  /**
   * Initializes a new instance of the <code>CarabinerMessageNode</code>
   * class.
   * 
   * @param userObject
   *          Component
   * @param icon
   *          Icon
   * @param highlightColor
   *          Color
   */
  public CarabinerComponentNode(Component userObject, Icon icon,
      Color highlightColor)
  {
    this(userObject, true, icon, highlightColor);
  }

  /**
   * Initializes a new instance of the <code>CarabinerMessageNode</code>
   * class.
   * 
   * @param userObject
   *          Component
   * @param allowsChildren
   *          boolean
   * @param icon
   *          Icon
   * @param highlightColor
   *          Color
   */
  protected CarabinerComponentNode(Component userObject,
      boolean allowsChildren, Icon icon, Color highlightColor)
  {
    super(userObject, allowsChildren);
    m_Component = userObject;
    m_Icon = icon;
    m_HighlightColor = highlightColor;
  }

  /**
   * Returns the component associated with the node.
   * 
   * @return Component
   */
  public Component getComponent()
  {
    return m_Component;
  }

  /**
   * Sets the icon to be displayed for the node in the tree view.
   * 
   * @param icon
   *          Icon
   */
  public void setIcon(Icon icon)
  {
    this.m_Icon = icon;
  }

  /**
   * Returns whether or not this node should be automatically expanded when
   * added to a tree
   * 
   * @return boolean
   */
  public boolean autoExpand()
  {
    for (int i = 0; i < getChildCount(); i++)
    {
      CarabinerComponentNode node = (CarabinerComponentNode) getChildAt(i);
      if (node.autoExpand())
      {
        return true;
      }
    }
    return false;
  }

  /**
   * Returns the nodes current icon.
   * 
   * @return Icon
   */
  public Icon getIcon()
  {
    return m_Icon;
  }

  public Color getHighlightColor()
  {
    return m_HighlightColor;
  }

  public String toString()
  {
    return ComponentUtils.getComponentName(getComponent());
  }
}
